package cn.edu.ctbu.scoremg.service;

import cn.edu.ctbu.scoremg.dao.ScoreRepository;
import cn.edu.ctbu.scoremg.entity.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreService {
    @Autowired
    public ScoreRepository scoreRepository;

    /**
     * 读所有教师列表
     *
     * @return List Teacher
     */
    public List<Score> findAll() {
        return scoreRepository.findAll();
    }

    public Page<Score> getAll(Pageable pageable) {
        return scoreRepository.findAll(pageable);
    }

    public Page<Score> getAll(Example<Score> example, Pageable pageable) {
        return scoreRepository.findAll(example, pageable);
    }

    /**
     * 按id进行查询
     *
     * @param id 主键，自增
     * @return
     */
    public Score findById(Integer id) {
        return scoreRepository.findById(id).get();
    }

    /**
     * 按学生姓名进行查询
     *
     * @param sname
     * @return
     */
    public List<Score> findByStu_name(String sname) {
        return scoreRepository.findBySname(sname);
    }

    /**
     * 按学生名字和课程名进行查询
     *
     * @param sname
     * @param cname
     * @return
     */
    public List<Score> findBySnameAndCname(String sname, String cname) {
        return scoreRepository.findBySnameAndCname(sname, cname);
    }

    /**
     * 新增
     */
    public Score insert(Score score) {
        return scoreRepository.save(score);
    }

    /**
     * 更新
     */
    public Score update(Score score) {
        return scoreRepository.save(score);
    }
    /**根据id删除*/
    public void delete(Integer id) {
        scoreRepository.deleteById(id);
    }

    /**根据id批量删除*/
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        scoreRepository.deleteByIds(ids);
    }
}

